package com.frxuexi.mysign

import android.app.DatePickerDialog
import android.app.DatePickerDialog.OnDateSetListener
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.DatePicker
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.frxuexi.lib_base.entity.SignBean
import com.frxuexi.lib_base.entity.SignBeanItem
import com.frxuexi.lib_base.entity.StatusBean
import com.frxuexi.lib_base.http.Const
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.mysign.adapter.MySignAdapter
import com.frxuexi.mysign.adapter.SignStatusAdapter
import com.frxuexi.mysign.databinding.ActivityMySignListBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import okhttp3.internal.notify
import java.util.Calendar

@Route(path = "/mysign/MySignListActivity")
@AndroidEntryPoint
class MySignListActivity : BaseActivity<ActivityMySignListBinding,MySignIntent,MySignViewModel>() {
    override val binding: ActivityMySignListBinding by lazy { ActivityMySignListBinding.inflate(layoutInflater) }

    override val vm: MySignViewModel by viewModels<MySignViewModel>()
    val signStatusAdapter by lazy { SignStatusAdapter() }
    val mySignAdapter by lazy { MySignAdapter() }
    val list= mutableListOf<SignBeanItem>()
    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        list.addAll(it.data as SignBean)
                        mySignAdapter.addData(list)
                        mySignAdapter.notifyDataSetChanged()
                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        val statusList= listOf<StatusBean>(
            StatusBean("全部",true),
            StatusBean("正常",false),
            StatusBean("迟到",false),
            StatusBean("早退",false),
            StatusBean("缺勤",false)
        )
        signStatusAdapter.addData(statusList)
        binding.signlistRvStatus.apply {
            adapter=signStatusAdapter
            layoutManager=LinearLayoutManager(this@MySignListActivity,RecyclerView.HORIZONTAL,false)
        }
        signStatusAdapter.setOnItemClickListener { adapter, view, position ->
            signStatusAdapter.data.forEach {
                it.isCheck=false
            }
            signStatusAdapter.data.get(position).isCheck=true
            signStatusAdapter.notifyDataSetChanged()

            mySignAdapter.apply {
                data.clear()
                if(position==0){
                    data.addAll(list)
                }else{
                    data.addAll(list.filter { it.status== statusList.get(position).status})
                }
                notifyDataSetChanged()
            }
        }

        binding.signlistRv.apply {
            adapter=mySignAdapter
            layoutManager=LinearLayoutManager(this@MySignListActivity)
        }
        vm.sendIntent(MySignIntent.getMySign(SPUtils.getInstance().getInt(Const.UID)))

        binding.signlistFilter.setOnClickListener {
            val calendar = Calendar.getInstance()
            DatePickerDialog(this,object :OnDateSetListener{
                override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
                    val m=if(month+1<10 ) "0${month+1}" else "${month+1}"
                    val d=if(dayOfMonth<10) "0${dayOfMonth}" else "${dayOfMonth}"
                    val date="${year}-${m}-${d}"
                    binding.signlistEt.setText(date)

                    val mylist =
                        mySignAdapter.data.filter { it.morningCheckIn.split(" ")[0] == date }
                    mySignAdapter.data.clear()
                    mySignAdapter.addData(mylist)
                    mySignAdapter.notifyDataSetChanged()
                }
            },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show()
        }

    }

}